﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Hongapo_Common;
using System.Data;

namespace Hongapo_Services.Product
{
   public class Comment
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Comment");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Hongapo_Model.Product.Comment model)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Comment(");
            strSql.Append("PID,UserName,Email,HomeURL,Content,Isopen,Favour,Oppose,ParentId,Createdate,Remark)");
            strSql.Append(" values (");
            strSql.Append("@PID,@UserName,@Email,@HomeURL,@Content,@Isopen,@Favour,@Oppose,@ParentId,@Createdate,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@PID", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@HomeURL", SqlDbType.VarChar,100),
					new SqlParameter("@Content", SqlDbType.NVarChar,300),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@Favour", SqlDbType.Int,4),
					new SqlParameter("@Oppose", SqlDbType.Int,4),
					new SqlParameter("@ParentId", SqlDbType.Int,4),
					new SqlParameter("@Createdate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.VarChar,50)};
            parameters[0].Value = model.PID;
            parameters[1].Value = model.UserName;
            parameters[2].Value = model.Email;
            parameters[3].Value = model.HomeURL;
            parameters[4].Value = model.Content;
            parameters[5].Value = model.Isopen;
            parameters[6].Value = model.Favour;
            parameters[7].Value = model.Oppose;
            parameters[8].Value = model.ParentId;
            parameters[9].Value = model.Createdate;
            parameters[10].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Hongapo_Model.Product.Comment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Comment set ");
            strSql.Append("PID=@PID,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Email=@Email,");
            strSql.Append("HomeURL=@HomeURL,");
            strSql.Append("Content=@Content,");
            strSql.Append("Isopen=@Isopen,");
            strSql.Append("Favour=@Favour,");
            strSql.Append("Oppose=@Oppose,");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("Createdate=@Createdate,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@PID", SqlDbType.Int,4),
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@HomeURL", SqlDbType.VarChar,100),
					new SqlParameter("@Content", SqlDbType.NVarChar,300),
					new SqlParameter("@Isopen", SqlDbType.Int,4),
					new SqlParameter("@Favour", SqlDbType.Int,4),
					new SqlParameter("@Oppose", SqlDbType.Int,4),
					new SqlParameter("@ParentId", SqlDbType.Int,4),
					new SqlParameter("@Createdate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.Email;
            parameters[4].Value = model.HomeURL;
            parameters[5].Value = model.Content;
            parameters[6].Value = model.Isopen;
            parameters[7].Value = model.Favour;
            parameters[8].Value = model.Oppose;
            parameters[9].Value = model.ParentId;
            parameters[10].Value = model.Createdate;
            parameters[11].Value = model.Remark;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Comment ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByPID(int PID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Comment ");
            strSql.Append(" where PID=@PID");
            SqlParameter[] parameters = {
					new SqlParameter("@PID", SqlDbType.Int,4)
};
            parameters[0].Value = PID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Comment ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Hongapo_Model.Product.Comment GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,PID,UserName,Email,HomeURL,Content,Isopen,Favour,Oppose,ParentId,Createdate,Remark from T_Comment ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            Hongapo_Model.Product.Comment model = new Hongapo_Model.Product.Comment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PID"].ToString() != "")
                {
                    model.PID = int.Parse(ds.Tables[0].Rows[0]["PID"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                model.HomeURL = ds.Tables[0].Rows[0]["HomeURL"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["Isopen"].ToString() != "")
                {
                    model.Isopen = int.Parse(ds.Tables[0].Rows[0]["Isopen"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Favour"].ToString() != "")
                {
                    model.Favour = int.Parse(ds.Tables[0].Rows[0]["Favour"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Oppose"].ToString() != "")
                {
                    model.Oppose = int.Parse(ds.Tables[0].Rows[0]["Oppose"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Createdate"].ToString() != "")
                {
                    model.Createdate = DateTime.Parse(ds.Tables[0].Rows[0]["Createdate"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PID,UserName,Email,HomeURL,Content,Isopen,Favour,Oppose,ParentId,Createdate,Remark ");
            strSql.Append(" FROM T_Comment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,PID,UserName,Email,HomeURL,Content,Isopen,Favour,Oppose,ParentId,Createdate,Remark ");
            strSql.Append(" FROM T_Comment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

    
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere,string sort)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    new SqlParameter("@sort", SqlDbType.VarChar,20)
                    };

            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = strWhere;
            parameters[3].Value = sort;
            return DbHelperSQL.RunProcedure("P_CommentByPager", parameters, "ds");
        }

        #endregion  Method
       /// <summary>
       ///  修改评论的启用状态
       /// </summary>
       /// <param name="Id"></param>
       /// <param name="Isopen"></param>
       /// <returns></returns>
        public bool UpdateIsopen(int Id, int Isopen)
        {
            string sql = "update T_Comment set isopen="+Isopen+" where ID="+Id+"";
            int row=DbHelperSQL.ExecuteSql(sql);
            if (row > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
